<?php include 'top.html' ?>

<?php
  $uname = $_POST['uname'];
  $password = $_POST['password'];

  require_once 'commonutils/db/DBConnectionManager.php';
  
  $sqlquery = "select count(*) as cnt from user where userName = '".$uname."' and password = '".$password."'";
  //echo $sqlquery."<br>";
  $result = DBConnectionManager::query($sqlquery);
  $row = mysql_fetch_assoc($result);
  $cnt = $row['cnt'];

  if($cnt > 0){
	  //echo "granted</br>";
  	  $sqlquery = "select userType, programCode from user where userName = '".$uname."'";
  	  $result = DBConnectionManager::query($sqlquery);
  	  $row = mysql_fetch_assoc($result);
  	  $userType = $row['userType'];
  	  $programCode = $row['programCode'];
  	  
  	  $year = $_POST['selectYear'];
	  $semesterName = $_POST['selectSemester'];
	  if($semesterName == 'Spring')
	    $semester = 1;
	  else if($semesterName == 'Summer')
	    $semester = 2;  
	  else
	    $semester = 3; 
	    
	  $semesterID = $year.$semester;
	  
	  session_start();
	  $_SESSION['authorized'] = 1;
	  $_SESSION['uname'] = $uname;
	  $_SESSION['userType'] = $userType;
	  $_SESSION['year'] = $year; 
	  $_SESSION['semester'] = $semester;
	  $_SESSION['semesterName'] = $semesterName;
	  $_SESSION['semesterID'] = $semesterID;
	  $_SESSION['programCode'] = $programCode;

  	  $sqlquery = "select shortName from program where programCode = '".$programCode."'";
  	  $result = DBConnectionManager::query($sqlquery);
  	  $row = mysql_fetch_assoc($result);
  	  $programShortName = $row['shortName'];

  	  $_SESSION['programShortName'] = $programShortName;
	  
  	  if($userType == 0)
	    header('Location: SecurityIndex.php') ;
	  else if($userType == 1)
	    header('Location: PCIndex.php') ;
	  else if($userType == 2)
	    header('Location: VPCIndex.php') ;
	  else if($userType == 3)
	    header('Location: SecretaryIndex.php') ;	    
	  else if($userType == 4)
	    header('Location: StudentIndex.php') ;	 
	  else
	    ;     	    
	    
  }
    
  else{
	//echo $uname. ":".$password;
	echo "<div align=center>";
    echo "<p><font color=red> <b> Wrong password </b></font> </br></p>";
    echo "<p><a href=\"Login.php\" >Return to Login page</a></p>";
    echo "</div>";
  }
    
    
  
?>



